<?php
#好友关系列表
class UserListModel extends PdoModel {
    protected $_name = 'user_list';

    function __construct(){
        parent::__construct();
    }

    #添加
    function addData($data){
        if(empty($data)){
            return false;
        }
        return $this->insert($data);
    }

    #删除
    function deleteData($id = 0){
        if(empty($id)){
            return false;
        }
        $row = $this->find("id = {$id}");
        if (empty($row)) {
            return false;
        }
        return $this->delete("id = {$id}");
    }

    #删除
    function deleteDataByUid($uid = 0){
        if(empty($uid)){
            return false;
        }
        $row = $this->find("uid = {$uid}");
        if (empty($row)) {
            return false;
        }
        return $this->delete("uid = {$uid}");
    }

    #删除
    function deleteDataByFriendId($friend_id = 0){
        if(empty($friend_id)){
            return false;
        }
        $row = $this->find("friend_id = {$friend_id}");
        if (empty($row)) {
            return false;
        }
        return $this->delete("friend_id = {$friend_id}");
    }

    #更新
    function updateDataByFriendid($data,$friend_id){
        return $this->update($data,"friend_id = {$friend_id}");
    }


    #更新
    function updateDataById($data,$id){
        return $this->update($data,"id = {$id}");
    }



    function getDataById($id){
        if(empty($id)){
            return array();
        }

        $sql = " select * from {$this->_name} where id={$id} limit 1";
        $result = $this->fetchRow($sql);

        return $result;
    }

    #查找好友关系
    function getFriends($uid, $friend_id){
        if(empty($uid) || empty($friend_id)){
            return array();
        }

        $sql = "select * from  {$this->_name} where uid={$uid} and friend_id={$friend_id} limit 1";
        $result = $this->fetchRow($sql);

        return $result;
    }


    #基本表信息
    function getListByAdmin($page = 1, $page_size =  20, $condition = array()){
        $sql = " select * from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and uid = {$condition['uid']}";
        }

        if(!empty($condition['friend_id'])) {
            $sql .= " and friend_id = {$condition['friend_id']}";
        }

        if(!empty($condition['status'])) {
            $sql .= " and status = {$condition['status']}";
        }

        $start = ($page -1 ) * $page_size;
        $sql .= " limit {$start}, {$page_size}";

        $data = $this->fetchAll($sql);
        return $data;
    }

    #基本表信息
    function getListByAdminCount($condition = array()){
        $sql = " select count(*) as num from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and uid = {$condition['uid']}";
        }

        if(!empty($condition['friend_id'])) {
            $sql .= " and friend_id = {$condition['friend_id']}";
        }
        if(!empty($condition['status'])) {
            $sql .= " and status = {$condition['status']}";
        }
        if(isset($condition['page_size'])) {
            $sql .= " limit {$condition['page_size']} ";
        }

        $result = $this->fetchRow($sql);
        $num = 0;
        if(!empty($result['num'])) {
            $num = $result['num'];
        }
        return $num;
    }


    /***
     * @param $uid 我的UiD
     * @param $friend_id 添加我的好友uid
     * @return bool
     */
    function makeFriends($uid, $friend_id){
        $my_date = $this->getFriends($uid, $friend_id); //查找之前是否绑定过好友关系记录
        $ret = $this->checkFriend($my_date, $uid, $friend_id);

        $friends_date = $this->getFriends($friend_id, $uid); //查找之前是否绑定过好友关系记录
        $ret = $this->checkFriend($friends_date, $friend_id, $uid);

        return true;
    }

    #核对双方是否是好友,不是的话添加互相为好友
    /***
     * @param $date
     * @param $friend_id
     * @return bool|Int
     */
    function checkFriend($date, $uid, $friend_id){
        $curtime = time();
        if(!empty($date)){ //之前绑定过好友
            if($date['status'] != 2){ //不是好友,更改为好友状态
                $update = array( 'status' => 2 );
                $ret = $this->updateDataById($update, $date['id']);
                return $ret;
            }

        }else{

            $user_model = new UserModel();
            $friend_info = $user_model->getUserInfoByUid($friend_id);
            $insert = array(
                'uid' => $uid,
                'friend_id' => $friend_id,
                'remark' => $friend_info['mobile'],
                'avatar' => $friend_info['avatar'],
                'status' => '2',
                'created_at' => $curtime,
                'updated_at' => $curtime,
            );

            $ret = $this->addData($insert);
            return $ret;
        }

        return true;
    }


}
